require "lfs"
require "unity"
local MPIModel = require "MPIModel"
local CreateRadome = require "luaCreateRadome"
local nl = require"luaNumeric"
local QGallop = require"QGMainWindow"
require "Q2DViewer"
local Q2DViewer = require "Q2DViewerEx"

-----------------------------------------------------------------------------
--初始化参数
local FSSRadomeGenerator = MPIModel:new( { 
	console_mode = false;
	p = 10; --频选周期
	nx = {10,40};
	xstart=0;
	xend=100;
} );
-----------------------------------------------------------------------------

-----------------------------------------------------------------------------
--[[创建表和创建元表
function RadomeAnalyzer:new (o)
	o = o or {};
	self.__index = self;
	setmetatable(o,self)
	return o;
end
--]]---------------------------------------------------------------------------

function FSSRadomeGenerator:create_viewer2D ( )
	return viewer;
end

-----------------------------------------------------------------------------
--导入模型
function FSSRadomeGenerator:load ( path )
	Geometry.clear_all();
	self.path = path;
	self.radome = Geometry.load_step(path);
	-- Geometry.AddShape(self.radome)
	self:perspective_view()
	print(path)
	
	--[[
	 self.analyzer = CreateRadome.Radome2DAnalyzer:new();
	 
	 assert(self.analyzer:import_from_file(path)>=0, "import file error!\n"..path );
	 
	 self:perspective_view()

	 assert(self.analyzer:isSolid(), "导入模型不包含实体!" );
	 assert(self.analyzer:numSolid()==1, "导入模型超过一个实体!" );
	 assert(self.analyzer:checkOrientation(), " 天线罩大端未朝向x轴正向" );
	 self:perspective_view()
	--]]
	
	-----------------------------------------------------
	-- import the radome model
	self.analyzer = CreateRadome.Radome2DAnalyzer:new();
	assert(self.analyzer:import_from_file(path)>=0, "Import file from:\n"..path );
	print( "导入模型并完成结构分析："..path );
	--assert(analyzer:isSolid(), "导入模型不包含实体!" );
	assert(self.analyzer:numSolid()<=1, "导入模型超过一个实体!" );
	print( "模型包含实体数量：".. self.analyzer:numSolid());
	print( "检查天线罩大端朝向x轴正向...");
	assert(self.analyzer:checkOrientation(), " 天线罩大端未朝向x轴正向" );
	print( "检查完成");
	 assert(self.analyzer:decomposite(), " 天线罩面元分析失败！" );
	-- assert(analyzer:decomposite_semi_radome(), " 天线罩面元分析失败！" );


	self.analyzer:intersection_with_xy_plane()
	assert(self.analyzer:regenerate_profile(), "重新生成外表面错误" );
	-- assert(analyzer:regenerate_from_xz_section(), "重新生成外表面错误" );
	-----------------------------------------------------


	self:perspective_view()
	
	self:generate_outer();
	
	self:create_cone_fss();

end
-----------------------------------------------------------------------------

-----------------------------------------------------------------------------
--创建锥罩频选结构
function FSSRadomeGenerator:generate_outer ( )

	self.xout = nl.new_vector();
	self.yout = nl.new_vector();
	assert( self.analyzer:pnts_outer(self.xout,self.yout)>=0, "outer error" );

	-----------------------------------------------------
	-- plot the porfile of radome
	---[[
	local mw = QGallop.getMainWindow();
	local view1 = mw:create2DView("profile view");
	gp1 = view1:addGraph(self.xout,self.yout);
	
	gp1:setProperty({ gp = gp1, name="外形面", color={1,0,0} })	
	view1.xAxis:setProperty({name="x 轴(mm)",linewidth=2,fontsize=14,tickfontsize=10})
	view1.yAxis:setProperty({name="y 轴(mm)",linewidth=2,fontsize=14,tickfontsize=10})
	view1.xAxis2:setProperty({name=stripfilename(self.path),linewidth=2,fontsize=14,tickfontsize=10})
	view1.yAxis2:setProperty({name=stripfilename(self.path),linewidth=2,fontsize=14,tickfontsize=10})

	view1:rescaleAxes();
	view1: setAspectRatio();
	-- view1:savePng(filename.."-profile.png")
	-- view1:exportData(filename.."-母线.txt")
	--]]
	-----------------------------------------------------

end
-----------------------------------------------------------------------------

-----------------------------------------------------------------------------
--创建分块边界
function FSSRadomeGenerator:create_cone_fss ( )
	local xout = self.xout;
	assert( #xout>0, "外形面格点数为0")
	local xsplit = nl.new_vector();
	local nsplit = ( xout[ #xout ] - xout[ 1 ] ) / (self.deltax or (self.p*5))
	for ii=1,nsplit-1 do
	    xsplit:add( ii* (self.deltax or (self.p*5) ) )
	end

	local nxsplit = nl.new_ivector( #self.nx )
	for ii=1,#self.nx do
	     nxsplit[ii] = self.nx[ii];
	end

	local  tsplit = nl.new_vector();
	tsplit :push_back(0);
	tsplit :push_back(math.pi/2);
	
	
	print( "start filling..." )
	self.generator = CreateRadome.FSSRadomeGenerator:new(self.p,1e-4);
	self.generator:fss_range(self.xstart,self.xend)
	self.generator:fss_theta_range(0,math.pi)
	self.generator:setDn(self.dN or 20)
	self.generator:create_outer( self.analyzer );
	-- self.generatorgenerator:set_external_shape( self.objExt );
	 local GLShapes = require"luaGLShapes";


	local unitGenerator = function( x,y,z)
		 local cir1 = GLShapes.create_circle( {radius={3,3} } )
		 local cir2 = GLShapes.create_circle( {radius={2,2} } )
		-- GLShapes.AddShape(GLShapes.join({cir1,cir2}))
		  return GLShapes.difference({cir1,cir2});
		-- return cir1;
	end
	self.generator:unitGenerator(unitGenerator);
	print( "    离散化表面线段" )
	assert( self.generator:build_equal_segments_pts_from_curve1()>=0, "离散化表面点错误（点数为零）！");
	self.generator:split_face_by_period_ndoes(nxsplit,tsplit);
  
	self.generator:fragment_external_surfaces();
  
  ---[[
	assert( self.generator:build_fss_units(),"创建频率旋转表面单元");
	self.generator:write_fss( self.path .. "-fss.stp" );
	self.generator:write_cfss( self.path .. "-cfss.stp" );
	self.generator:write_combined_fss( self.path .. "-all.stp" );
	self.generator:write_fss_segments( self.path .. "-fss-segments(%d).stp" );
	self.generator:write_cfss_segments( self.path .. "-cfss-segments(%d).stp" );
	self.generator:create_mesh( 1.5 );
	self.generator:export_mesh( self.path .. "-msh.bdf" );
	--]]
	
	-- generator:export_mesh( filename .. "-msh.msh" );
	-- generator:export_mesh( filename .. "-msh.inp" );
	-- generator:export_mesh( filename .. "-msh.mesh" );
	-- generator:export_mesh( filename .. "-msh.unv" );

end
-----------------------------------------------------

return FSSRadomeGenerator;
